<?
Class Root extends CI_Controller {
	public $data;

	public function __construct() {
		parent::__construct();
		$this -> data['header'] = "please set header in method";
		$this -> checkLoginStatus();

		$this -> load -> model("usermodel");
		if ($this -> getLoginStatus()) {
			
			$myAuthen = $this -> usermodel -> getMyAuthen($this -> getUserID());
			if (!empty($myAuthen)) {
				
				$this -> session -> set_userdata("AUTHEN", $myAuthen);
			}
		}
		$this -> data['pjs'] = $this -> config -> item("base_url") . "asset/js/";
		$this -> data['js'] = "";
		$this -> data['image'] = base_url() . "asset/images/";
		$this -> data['pcss'] = $this -> config -> item("base_url") . "asset/css/";
		$this -> data['current'] = $this -> getCurrentController();
		$this -> data['jsFile'] = array("myfunc");
		$this -> load -> library('paginationlib');
	}

	public function initPagination($allRow) {

		parse_str($_SERVER['QUERY_STRING'], $q);
		unset($q['per_page']);
		unset($q['qu']);
		if (!empty($q)) {
			$q = array_filter($q);
		}
		$quer = http_build_query($q);
		if ($quer != "") {
			$quer = "&" . $quer;
		}
		
		$url = uri_string() . "/?qu=true" . $quer;
		
		return $this -> paginationlib -> initPagination($url, $allRow);

	}

	public function getCurrentController($controller = '') {
		if ($controller == '') {

			return base_url() . $this -> uri -> segment(1) . "/";
		} else {
			return base_url() . $controller;
		}
	}

	public function checkLoginStatus() {
			
		if ($this -> uri -> segment(1) == "user") {
			$method = $this -> uri -> segment(2);
			$method = strtolower($method);
			if ($method == "login" || $method == "logout" || $method == "loginaction") {
				return true;
			}
		}
		
		$login = $this -> session -> userdata("islogin");
		if ($login == "") {
			
			redirect("/user/login");
			exit();
		}
	}

	public function checkAuthen($role= 'dashboard',$authenName='') {
		
		
		if(strtolower($this->getUserLevel())=="admin"){
			return true;
		}
		if ($this -> uri -> segment(1) == "user") {
			$method = $this -> uri -> segment(2);
			$method = strtolower($method);
			if ($method == "login" || $method == "logout" || $method == "loginaction") {
				return true;
			}
		}

		if ($authenName == "dashboard") {
			return true;
		}
		
		if (strpos($authenName, "root") !== FALSE) {
			return true;
		}
		
		$authenThisPage = false;
		$isViewPage = false;

		if (strpos($authenName, "view") !== FALSE) {
			$isViewPage = true;
		}

		$authen = $this -> session -> userdata("AUTHEN");
		
		foreach ($authen as $in => $val) {
				
			if($val->AuthenRoleName==$role."-root"){
				
				return true;
			}
			if (!$isViewPage) {
				
				if($val->AuthenRoleName==$role."-".$authenName){
					return true;
				}
			} else {
				
				//echo $val -> AuthenRoleName . " " . $authenName . " <br />";
				if (strpos($val -> AuthenRoleName, $role."-".$authenName) !== FALSE) {
					return true;
				}
			}
		}
	//print_r($authen);exit();
		if (!$authenThisPage) {
			
			redirect(base_url());
			
		}

	}
	public function isAdmin() {
			
		if(strtolower($this->getUserLevel())=="admin"){
			return true;
		}
		return false;
	}
	public function canUseFunction($role='dashboard',$authenName='') {
			
		if(strtolower($this->getUserLevel())=="admin"){
			return true;
		}
		
		$can = false;
		$authen = $this -> session -> userdata("AUTHEN");
		foreach ($authen as $in => $val) {
			if($val->AuthenRoleName==$role."-root"){
				return true;
			}
			if ($val -> AuthenRoleName == $role."-".$authenName) {

				$can = true;
				break;

			}

		}
		return $can;
	}

	public function getSaleID() {
		return $this -> session -> userdata("sale_no");
	}

	public function setSaleID($saleNo) {
		$this -> session -> set_userdata("sale_no", $saleNo);
	}

	public function getLoginStatus() {
		
		return $this -> session -> userdata("islogin");
	}

	public function setLoginStatus() {
		$this -> session -> set_userdata("islogin", true);
	}

	public function getSaleName() {
		return $this -> session -> userdata("sale_name");
	}

	public function setSaleName($sale_name) {
		$this -> session -> set_userdata("sale_name", $sale_name);
	}

	public function getUserLevel() {
		return $this -> session -> userdata("level_id");
	}

	public function setUserLevel($levelID) {
		$this -> session -> set_userdata("level_id", $levelID);
	}

	public function getUserID() {
		return $this -> session -> userdata("userID");
	}

	public function setUserID($userID) {
		$this -> session -> set_userdata("userID", $userID);
	}

	public function includeView($view) {
		$this -> load -> view("header", $this -> data);
		$this -> load -> view($view, $this -> data);
		$this -> load -> view("footer", $this -> data);
	}

	public function checkIDSearch($authenName) {

		$viewAllData = $this -> canUseFunction($authenName);
		if ($viewAllData) {
			return "";
		} else {
			return $this -> getSaleID();
		}

	}

	public function addJS($array) {

		$this -> data['jsFile'] = array_merge($this -> data['jsFile'], $array);

		$js = "";
		foreach ($this->data['jsFile'] as $in) {
			$js .= '<script type="text/javascript" src="' . $this -> data['pjs'] . $in . '.js"></script>';
		}
		$this -> data['js'] = $js;
	}

	public function Post2Array($array) {
		$data = array();
		foreach ($array as $in => $val) {
			$d = $_POST[$in];
			if (!is_array($d)) {
				$data[$in] = addslashes($d);
			} else {
				$data[$in] = $d;
			}
		}
		return $data;
	}

	public function set_res($flag, $message) {
		echo json_encode(array("flag" => $flag, "message" => $message));
		exit();
	}

	public function checkDataCheckbox($key, $default = "N") {
		return (isset($_POST[$key])) ? $_POST[$key] : $default;
	}

	public function getCoreQuery($canSearch = false) {
		
		parse_str($_SERVER['QUERY_STRING'], $q);
		unset($q['per_page']);
		unset($q['qu']);
		
		if (!empty($q)) {
			if ($canSearch) {
				
				return array_filter($q);
			} else {
				return array();
			}
		} else {
			return array();
		}

	}

}
?>